'use strict';

const { BaseParameter, stateKeys } = require('../core/base_mongo_model');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const FeedbackSchema = new Schema({
    ...BaseParameter,
    /**
    * 反馈者名称
    */
    name: { type: String, default: '' },
    /**
    * 反馈者名称
    */
    icon: { type: String, default: '' },
    /**
     * 反馈者手机号
     */
    phone: { type: String, default: '' },
    /**
     * 反馈者QQ号
     */
    qq: { type: String, default: '' },
    /**
     * 反馈者微信
     */
    wx: { type: String, default: '' },
    /**
     * 是否显示在意见列表
     */
    isShow: { type: Boolean, default: false },
    /**
     * 反馈时间
     */
    date: { type: Date },
    /**
     * 反馈内容
     */
    content: { type: String, default: '' },
  });

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取查询参数
   */
  FeedbackSchema.statics.getParmas = function(data) {
    const params = {
      $or: [
        { stateKey: stateKeys.NORMAL },
        { stateKey: { $exists: false } },
      ],
      ...data,
    };
    return params;
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取列表数据
   */
  FeedbackSchema.statics.findList = function(data) {
    const params = this.getParmas(data);
    const skip = (data.pageNum - 1) * data.pageSize;
    const model = this.find(params).skip(skip).limit(data.pageSize);
    model.sort({ date: -1 });
    return model.exec();
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取个数
   */
  FeedbackSchema.statics.findCount = function(data) {
    const params = this.getParmas(data);
    return this.countDocuments(params);
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取个数
   */
  FeedbackSchema.statics.add = function(data) {
    return this(data).save();
  };

  /**
   * 删除单个数据
   * @param id 要删除数据的ID
   * @param thoroughly 是否测地删除数据
   * @returns {*}
   */
  FeedbackSchema.statics.removeModel = function(id, thoroughly = false) {
    if (thoroughly) {
      return this.deleteOne({
        _id: mongoose.Types.ObjectId(id),
      });
    }
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      stateKey: stateKeys.DUSTBIN,
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 更新数据
   */
  FeedbackSchema.statics.updateModel = function(id, upData) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      isShow: upData.isShow,
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 从回收站还原
   */
  FeedbackSchema.statics.dustbinRestore = function(id) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      stateKey: stateKeys.NORMAL,
    });
  };

  return mongoose.model('Feedback', FeedbackSchema, 'feedbacks');
};
